package cn.yi.medicalsys.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import cn.yi.medicalsys.pojo.Register;
import cn.yi.medicalsys.service.RegisterService;
import cn.yi.medicalsys.service.impl.RegisterServiceImpl;

public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	//显示
	public void pageShow(HttpServletRequest request, HttpServletResponse response,String date) throws IOException{
		PrintWriter out = response.getWriter();
		RegisterService register=new RegisterServiceImpl();		
		List<Register> list=register.findRegisterBydate(date);
		String json=JSON.toJSONStringWithDateFormat(list, "yyyy-MM-dd HH:mm:ss");
		out.print(json);
	}
	
	//修改
	public void updateStatus(HttpServletRequest request, HttpServletResponse response,String idreg) throws IOException{
		PrintWriter out = response.getWriter();
		RegisterService register=new RegisterServiceImpl();
		Integer idre=null;
		if(idreg!=null && !idreg.equals("")){
			idre=Integer.valueOf(idreg);
		}
		int row=register.updateRegisterByNo(idre);
		if(row==1){
			out.print("<script>alert('接收成功');history.back()</script>");
		}else{
			out.print("<script>alert('接收失败');history.back()</script>");
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String action = request.getParameter("action");
		String date=request.getParameter("date");
		String idreg=request.getParameter("idreg");		
		System.out.println(111111);
		if (action != null && !action.equals("")) {
			if (action.equals("pageshow")) {
				this.pageShow(request, response,date);
			}else if(action.equals("modify")){
				this.updateStatus(request, response,idreg);
			}
		}else{
			out.print("<script>alert('action 为空');history.back()</script>");
		}		
		out.flush();
		out.close();
	}

	public void init() throws ServletException {
	}

}
